<template>
    <div class="full clearfix box-border" style="background: #F5F7FA;padding-top: 50px">
      <add-paper-left class="fl" v-model="contest.ques" />
      <add-paper-right class="fr" :contest="contest" />
    </div>
</template>

<script>
import AddPaperLeft from './AddPaperLeft'
import AddPaperRight from './AddPaperRight'
import api from '@/api'
import __ from 'lodash'
export default {
  mounted () {
    this.$bus.$on('save-paper', () => {
      // 深拷贝
      let contest = { ...this.contest }
      contest.ques = []
      this.contest.ques.forEach((item) => {
        contest.ques.push({ ...item })
      })
      if (__.isEmpty(contest.name)) {
        this.$message.error('考试名称不能为空！')
        return
      }

      if (__.isEmpty(contest.startTime) || __.isEmpty(contest.endTime)) {
        this.$message.error('请选择考试时间！')
        return
      }

      if (contest.timeLen == null) {
        this.$message.error('请选择考试时长！')
        return
      }

      contest.timeLen = '2010-01-21 ' + contest.timeLen + ':00'
      let sumScore = 0
      for (let i = 0; i < contest.ques.length; i++) {
        let item = contest.ques[i]
        let count = item.simpleCount + item.complexCount + item.normalCount
        if (count === 0) {
          this.$message.error('大题至少有一道题目！')
          return
        }

        if (__.isEmpty(item.category)) {
          this.$message.error('大题必须选择题目分类！')
          return
        }

        let perScore = parseInt(item.perScore)

        if (item.perScore === '' || perScore === 0) {
          this.$message.error('题目的分数不能为0！')
          return
        }

        if (__.isEmpty(item.title)) {
          item.title = this.$converters.convertType(item.type)
        }

        sumScore += (count * perScore)
      }

      console.log(sumScore)
      contest.sumScore = sumScore

      if (contest.sumScore === 0) {
        this.$message.error('考试总分不能为0！')
        return
      }

      api.createContest(contest)
        .then(api.commonResp((success, data) => {
          if (success) {
            this.$message('创建考试成功！')
            this.$router.push({ name: 'index' })
          } else {
            this.$message.error('创建考试失败')
          }
        }))
    })
    this.$bus.$on('created-ques', (ques) => {
      this.contest.ques.push(ques)
    })
    this.$bus.$on('op-del-ques', (index) => {
      this.contest.ques.splice(index, 1)
    })
    this.$bus.$on('op-up-ques', (index) => {
      let temp = this.contest.ques.splice(index, 1)
      this.contest.ques.splice(index - 1, 0, ...temp)
    })
    this.$bus.$on('op-down-ques', (index) => {
      let temp = this.contest.ques.splice(index, 1)
      this.contest.ques.splice(index + 1, 0, ...temp)
    })
    let name = this.$store.state.paper.contest.name
    if (!__.isEmpty(name)) {
      this.contest.name = name
    }
  },
  data () {
    return {
      contest: {
        name: '',
        startTime: null,
        endTime: null,
        timeLen: null,
        ques: [
          // {
          //   title: '',
          //   type: 1,
          //   categories: [],
          //   simpleCount: 0,
          //   normalCount: 0,
          //   complexCount: 0,
          //   perScore: 0
          // }
        ],
        sumScore: 0
      }
    }
  },
  components: {
    AddPaperRight,
    AddPaperLeft
  }
}
</script>

<style scoped lang="less">

</style>
